/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.general.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author anagnosg
 */
public class PropertiesUtils {
     
    
    public static Properties loadProperties(String propertiesFile,boolean escape) {
 
	FileReader fr = null;
        try {
            fr = new FileReader(propertiesFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PropertiesUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return loadProperties(fr,escape);
  }
    public static Properties loadProperties(FileReader input,boolean escape) {
 
	Properties prop = new Properties();
	
 
	try {
                //input=Properties.class.getResourceAsStream(propertiesFile);
	        if(input!=null){
		// load a properties file
                    String fStr = FileUtils.readTextFile(input);
                    if(escape){
                    prop.load(new StringReader(fStr.replace("\\","\\\\")));
                    }
                    else{
                        prop.load(new StringReader(fStr));
                    }
                }
                else{
                    System.out.println("Properties File is null");
                }
 
		 
	} catch (IOException ex) {
		ex.printStackTrace();
	} finally {
		if (input != null) {
                    try {
                        input.close();
                    } catch (IOException ex) {
                        Logger.getLogger(PropertiesUtils.class.getName()).log(Level.SEVERE, null, ex);
                    }
		}
	}
        return prop;
  }
    
  public static void printAllProperties(Properties prop){
      prop.list(System.out);
  }  
  
  
  
  public static void clearProperies(String propFile){
  File f = new File(propFile);
          Properties prop = new Properties();
          OutputStream output = null;
          if(f.exists()){
              prop=PropertiesUtils.loadProperties(propFile,false);
              prop.clear();
          }  
          try {
                  output = new FileOutputStream(propFile);
                  
                  // save properties to project root folder
                  prop.store(output, null);
          } catch (IOException io) {
                  io.printStackTrace();
          } finally {
                  if (output != null) {
                          try {
                                  output.close();
                          } catch (IOException e) {
                                  e.printStackTrace();
                          }
                  }

          }
  
  }
  public static void saveProperty(String propFile, String propertyName,String propertyValue){
          File f = new File(propFile);
          Properties prop = new Properties();
          OutputStream output = null;
          if(f.exists()){
              prop=PropertiesUtils.loadProperties(propFile,false);

          }  
          try {
                  output = new FileOutputStream(propFile);
                  // set the properties value
                  prop.setProperty(propertyName, propertyValue);
                  // save properties to project root folder
                  prop.store(output, null);
          } catch (IOException io) {
                  io.printStackTrace();
          } finally {
                  if (output != null) {
                          try {
                                  output.close();
                          } catch (IOException e) {
                                  e.printStackTrace();
                          }
                  }

          }
  }
}
